package liurong.driver.lock;

import java.util.concurrent.TimeUnit;

/**
 * synchronized介绍：
 * 
 * synchronized作用在普通方法上对类的对象生效
 * 
 * synchronized作用在静态方法上对类生效，与对象无关
 * 
 * @author liurong
 *
 */
public class LockTest {

	public static synchronized void m1() {
		try {
			System.out.println("方法一运行");
			TimeUnit.SECONDS.sleep(2);
			System.out.println("方法一运行结束");
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	public static synchronized void m2() {
		try {
			System.out.println("方法二运行");
			TimeUnit.SECONDS.sleep(2);
			System.out.println("方法二运行结束");
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	public static void main(String[] args) {
		final LockTest lockTest = new LockTest();
		final LockTest lockTest2 = new LockTest();
		new Thread(new Runnable() {
			@Override
			public void run() {
				lockTest.m1();
			}
		}).start();
		new Thread(new Runnable() {
			@Override
			public void run() {
				lockTest2.m2();
			}
		}).start();
	}
}
